package org.tony.spider.common.cmd;

import java.lang.reflect.Method;

import org.tony.spider.common.Command;

public class FetchCommandInvoker
    implements Invoker
{
  Boolean initial = false;
  Method  select  = null;

  /**
   * invoke the select method in target object
   */
  public Object invoke(Command cmd, Object target) throws Exception
  {
    synchronized (initial)
    {
      if (select == null)
      {
        select = target.getClass().getMethod("select", String.class);
      }
    }
    Object result = target;
    for (String lab : cmd.getLables())
    {
      result = select.invoke(result, lab);
    }
    return result;
  }
}
